package com.lyy.controller;


import com.github.pagehelper.PageInfo;
import com.lyy.entity.Order;
import com.lyy.entity.OrderDetail;
import com.lyy.entity.param.OrderBuyParam;
import com.lyy.result.Result;
import com.lyy.result.ResultCode;
import com.lyy.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author Liangyaoyun
 */
//@RestController=@Controller+@ResponseBody(返回json数据)
@RestController
@RequestMapping("/api/v1/order")
@Tag(name = "Order接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Operation(summary = "下单购买商品")
    @PostMapping("/buy")
    public Result buy(@RequestBody @Validated OrderBuyParam param, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        // 准备订单实体
        Order order = new Order();
        order.setOrderInfo(param.getOrderInfo());
        order.setTotalFee(param.getTotalFee());
        Order result = orderService.buy(order, userId, param.getVideoIds());
        return new Result(result);
    }

    @Operation(summary = "删除整个订单")
    @PostMapping("/deleteByOrderId/{orderId}")
    public Result deleteByOrderId(@PathVariable Integer orderId) {
        orderService.deleteByOrderId(orderId);
        return new Result(ResultCode.SUCCESS);
    }
    @Operation(summary = "分页查询订单明细")
    @GetMapping("/pageOrderDetail")
    public Result pageOrderDetail(@RequestParam Integer pageNum, @RequestParam Integer pageSize, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        PageInfo<OrderDetail> result = orderService.pageOrderDetail(pageNum, pageSize, userId);
        return new Result(result);
    }

    @Operation(summary = "删除一条订单明细")
    @PostMapping("/deleteOrderDetail/{orderDetailId}")
    public Result deleteOrderDetail(@PathVariable Integer orderDetailId) {
        orderService.deleteOrderDetail(orderDetailId);
        return new Result(ResultCode.SUCCESS);
    }
}
